package com.ktjy.web.servlet;

import com.ktjy.web.entity.AjaxResult;
import com.ktjy.web.entity.BannerItem;
import com.ktjy.web.entity.Page;
import com.ktjy.web.service.BannerService;
import com.ktjy.web.service.impl.BannerServiceImpl;
import my.framework.anno.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class BannerServlet{
    @RequestMapping("/banner")
    public Object doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        String opr = request.getParameter("opr");
        BannerService tmp = new BannerServiceImpl() {
        };
        Page<BannerItem> page=new Page<>();

        if("page".equals(opr)){
            String type=request.getParameter("name");

            if (type==""){
                type="0";
            }

            int pageNo=Integer.parseInt(request.getParameter("pageNo"));
            int pageSize=Integer.parseInt(request.getParameter("pageSize"));

            page.setPageSize(pageSize);
            page.setCurrPageNo(pageNo);
            page.setTotalCount(tmp.count());
            List<BannerItem> list=tmp.page(Integer.parseInt(type),(pageNo-1)*pageSize,pageSize);

            page.setList(list);

            return AjaxResult.success("查询成功",page);

        }else if("del".equals(opr)){
            int id=Integer.parseInt(request.getParameter("id"));

            int num=tmp.deleteById(id);

            return AjaxResult.success("执行成功",num);

        }else if("detail".equals(opr)){

            int id=Integer.parseInt(request.getParameter("id"));

            BannerItem data=tmp.selectById(id);

            return AjaxResult.success("执行成功",data);
        }else if("update".equals(opr)){
            int id=Integer.parseInt(request.getParameter("id"));
            int type=Integer.parseInt(request.getParameter("type"));
            String img_url=request.getParameter("imgUrl");
            String href_url=request.getParameter("hrefUrl");

            BannerItem bannerItem=new BannerItem();

            bannerItem.setId(id);
            bannerItem.setType(type);
            bannerItem.setImgUrl(img_url);
            bannerItem.setHrefUrl(href_url);

            int num =tmp.update(bannerItem);

            return AjaxResult.success("执行成功",num);

        }else if("insert".equals(opr)){
            int type=Integer.parseInt(request.getParameter("type"));
            String img_url=request.getParameter("imgUrl");
            String href_url=request.getParameter("hrefUrl");

            BannerItem bannerItem=new BannerItem();

            bannerItem.setType(type);
            bannerItem.setImgUrl(img_url);
            bannerItem.setHrefUrl(href_url);

            int num =tmp.insert(bannerItem);

            return AjaxResult.success("执行成功",num);

        }

        return null;
    }

}
